﻿using ShiQuan.IotDevice.Entity;
using ShiQuan.Provider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShiQuan.IotDevice.Business
{
    /// <summary>
    /// 设备基础业务处理
    /// </summary>
    public class BaseIotDeviceBLL<T> : BasicIotDeviceBLL<T> where T : BasicIotDeviceEntity, new()
    {
        /// <summary>
        /// 数据保存
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
        public override void SaveForm(string keyValue, T entity)
        {
            if(string.IsNullOrEmpty(keyValue))
            {
                if(string.IsNullOrEmpty(entity.Id))
                    entity.Id = Guid.NewGuid().ToString();

                entity.CreateDate = DateTime.Now;
                entity.CreateUserId = OperatorProvider.Provider.Current().UserId;
                entity.CreateUserName = OperatorProvider.Provider.Current().UserName;

                entity.EnterpriseId = OperatorProvider.Provider.Current().CompanyId;
                entity.EnterpriseName = OperatorProvider.Provider.Current().CompanyName;

                this.Repository.Insert<T>(entity);
            }
            else
            {
                entity.Id = keyValue;

                entity.ModifyDate = DateTime.Now;
                entity.ModifyUserId = OperatorProvider.Provider.Current().UserId;
                entity.ModifyUserName = OperatorProvider.Provider.Current().UserName;

                this.Repository.Update<T>(entity);
            }
        }
    }
}
